<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="/webjars/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="/static/js/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="/webjars/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <form>
        <div class="form-group card-header" style="text-align: center;"><h3>登录</h3></div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">账户</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="username" placeholder="账户">
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">密码</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="password" placeholder="密码">
            </div>
        </div>
        <div class="form-group row">
            <div onclick="login()" class="col-sm-6 text-center"><input type="button" value="登录" class="btn btn-primary">
            </div>
            <div onclick="register()" class="col-sm-6 text-center"><input type="button" value="注册" class="btn btn-info">
            </div>
        </div>
    </form>
</div>
<script th:inline="javascript">
    function login() {
        const json = {
            username: $("#username").val(),
            password: $("#password").val()
        };
        $.ajax({
            url: "/login",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(json),
            success: function (data) {
                if ("fail" != data) {
                    $.ajax({
                        url: "/getAll",
                        type: "GET",
                        headers: {
                            token: data
                        },
                        success: function (data) {
                            if (data == "success") {
                                window.location.href = "/index";
                            } else {
                                window.location.href = "/error";
                            }
                        }
                    });
                } else {
                    alert("登录失败：" + data);
                }
            }
        })
    }

    function register() {
        this.window.location.href = "/register";
    }
</script>
</body>
</html>